/**
 *
 */
package org.suren.littlebird.views.provider;

import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.suren.littlebird.Activator;
import org.suren.littlebird.po.SuRenBundle;

/**
 * @author suren
 *
 */
public class SuRenBundleFieldProvider extends LabelProvider implements
		ITableLabelProvider {
	public static final String RESOLVED = "RESOLVED";
	public static final String INSTALLED = "INSTALLED";
	public static final String UNKNOW = "UNKNOW";
	public static Image IMAGE_RESOLVED = null;
	public static Image IMAGE_INSTALLED = null;
	public static Image IMAGE_UNKNOW = null;

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang
	 * .Object, int)
	 */
	@Override
	public Image getColumnImage(Object ele, int colIndex) {
		if (ele instanceof SuRenBundle) {
			SuRenBundle bundle = (SuRenBundle) ele;

			switch (colIndex) {
			case 0:
				return imageCover(bundle.getState());
			default:
				return null;
			}
		}

		return null;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang
	 * .Object, int)
	 */
	@Override
	public String getColumnText(Object ele, int colIndex) {
		if (ele instanceof SuRenBundle) {
			SuRenBundle bundle = (SuRenBundle) ele;

			switch (colIndex) {
			case 0:
				return String.valueOf(bundle.getId());
			case 1:
				return bundle.getName();
			case 2:
				return bundle.getVersion();
			case 3:
				return stateCover(bundle.getState());
			}
		}

		return null;
	}

	/**
	 * @param state
	 * @return
	 */
	private String stateCover(int state) {
		switch (state) {
		case SuRenBundle.ACTIVE:
			return "active";
		case SuRenBundle.INSTALLED:
			return INSTALLED;
		case SuRenBundle.UNINSTALLED:
			return "UNINSTALLED";
		case SuRenBundle.RESOLVED:
			return RESOLVED;
		case SuRenBundle.STARTING:
			return "STARTING";
		case SuRenBundle.STOPPING:
			return "STOPPING";
		default:
			return UNKNOW;
		}
	}

	private Image imageCover(int state) {

		Display display = Display.getDefault();
		switch (state) {

		case SuRenBundle.INSTALLED:
			if (null == IMAGE_INSTALLED) {
				IMAGE_INSTALLED = new Image(display, Activator
						.getImageDescriptor("icons/warn2.png").getImageData());
			}
			return IMAGE_INSTALLED;
		case SuRenBundle.RESOLVED:
			if (null == IMAGE_RESOLVED) {
				IMAGE_RESOLVED = new Image(display, Activator
						.getImageDescriptor("icons/warn1.png").getImageData());
			}
			return IMAGE_RESOLVED;
		default:
			return null;
		}

	}

}
